﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using SMS.Common.Silverlight;
using System.ComponentModel;
using SMS.Entities;
using SMS.Controls.BranchSVC;
using SMS.Entities.Master;

namespace SMS.Controls
{
    public partial class SMSBranch : UserControl
    {
        private ServiceProxy<BranchServiceClient> proxy = null;
        public SMSBranch()
        {
            InitializeComponent();
            if (!DesignerProperties.IsInDesignTool)
            {
                proxy = new ServiceProxy<BranchServiceClient>("Service/BranchService.svc");
                proxy.Client.SearchBranchCompleted += new EventHandler<SearchBranchCompletedEventArgs>(Client_SearchBranchCompletedCompleted);
                smsAutoComplete1.SelectionChanged += new Action<object, object>(smsAutoComplete1_SelectionChanged);
                smsAutoComplete1.SearchTextChanged += new Action<string>(smsAutoComplete1_SearchTextChanged);
                smsAutoComplete1.TextChanged += new Action<string>(smsAutoComplete1_TextChanged);
                smsAutoComplete1.SetFilter(SearchCategory);
                smsAutoComplete1.SetDataTemplate(this.Resources["BranchTemplate"] as DataTemplate);
                smsAutoComplete1.SetValuePath("BranchName");
            }
        }

        void smsAutoComplete1_TextChanged(string obj)
        {
            SearchText = obj;
        }

        void Client_SearchBranchCompletedCompleted(object sender, SearchBranchCompletedEventArgs e)
        {
            smsAutoComplete1.ItemSource = e.Result as IEnumerable<SMS.Entities.BaseClasses.BaseEntity>;
            VisualStateManager.GoToState(smsAutoComplete1, "Normal", true);
        }


        private bool IsNewSearchTextRequired(string searchText)
        {
            return currentSearchText.ToLower() != searchText.ToLower();
        }
        private string currentSearchText = string.Empty;

        public bool SearchCategory(string search, object value)
        {
            return (value as SMS.Entities.Master.Branch).Branch_ID.ToString().Contains(search.ToLower()) ||
                (value as SMS.Entities.Master.Branch).BranchName.ToLower().Contains(search.ToLower());
            //return true;
        }

        void smsAutoComplete1_SearchTextChanged(string obj)
        {

            if (IsNewSearchTextRequired(obj))
            {
                currentSearchText = smsAutoComplete1.autoCompleteBox1.Text;
                VisualStateManager.GoToState(smsAutoComplete1, "BusyState", true);
                proxy.Client.SearchBranchAsync(string.Empty, obj);
            }
            SearchText = obj;
        }

        void smsAutoComplete1_SelectionChanged(object arg1, object arg2)
        {
            SelectedBranch = arg2 as Branch;
        }

        public Branch SelectedBranch
        {
            get { return (Branch)GetValue(SelectedBranchProperty); }
            set
            {
                SetValue(SelectedBranchProperty, value);
                smsAutoComplete1.SetSelectedItem(value);
            }
        }

        // Using a DependencyProperty as the backing store for SelectedProduct.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty SelectedBranchProperty =
            DependencyProperty.Register("SelectedBranch", typeof(Branch), typeof(SMSBranch), new PropertyMetadata(null, new PropertyChangedCallback(SelectedCategoryChanged)));

        public static void SelectedCategoryChanged(object sender, DependencyPropertyChangedEventArgs e)
        {
            var control = sender as SMSBranch;
            //if (control.SelectedProduct != e.NewValue as SMS.Entities.Product)
            //{
            control.SelectedBranch = e.NewValue as Branch;
            //}
        }

        public string SearchText
        {
            get { return (string)GetValue(SearchTextProperty); }
            set { SetValue(SearchTextProperty, value); }
        }

        // Using a DependencyProperty as the backing store for SearchText.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty SearchTextProperty =
            DependencyProperty.Register("SearchText", typeof(string), typeof(SMSBranch), new PropertyMetadata(string.Empty, new PropertyChangedCallback(SearchTextPropertyChanged)));

        private static void SearchTextPropertyChanged(object sender, DependencyPropertyChangedEventArgs e)
        {
            var control = sender as SMSBranch;
            control.SearchText = e.NewValue as string;
        }

    }
}
